package com.lss.b_doublePointer;

/**
 * @author lss
 * @description 接雨水
 * @createDate 2024/11/30-23:15
 */
public class lc_42 {
    public static void main(String[] args) {

    }

    /**
     * 上山 或者是 下山的视角
     *
     * @param height 高度数组
     * @return 最多雨水数量
     */
    public int trap(int[] height) {
        int left = 0;
        int right = height.length - 1;
        int pre_height = 0;
        int surf_height = 0;
        int ans = 0;
        while (left < right) {
            surf_height = Math.max(surf_height, height[right]);
            pre_height = Math.max(pre_height, height[left]);
            if (pre_height < surf_height) {

                ans += pre_height - height[left];
                left++;
            }else {
                ans += surf_height - height[right];
                right--;
            }
        }

        return ans;
    }
}
